/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.losalpes.marketplace.pitrack.beans;

import co.com.losalpes.marketplace.pitrack.bos.TrackBO;
import co.com.losalpes.marketplace.pitrack.entities.Track;
import co.com.losalpes.marketplace.pitrack.entities.persistence.Persistence;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
//import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author marketplace
 */
@Stateful
public class TRACKBeanTest implements TRACKRemote, TRACKLocal {

    @Override
    public String crearTrack(TrackBO trackBO) {
        String numero = "TRACK"+System.currentTimeMillis();
        trackBO.setIdTransaccion(numero);
        Track track = new Track(trackBO);
        this.getEm().persist(track);
        return numero;
    }

    /**
     * Busco el último track, siendo diferentes los fabricantes
     * 
     * @param nit
     * @return 
     */
    public List<TrackBO> buscarPorUltimoTimeIdTransaccionDiferente(String nit) {
        
        TrackBO trackBO = null;
        List<TrackBO> trackList = new ArrayList<TrackBO>(0);
        List<Track> tracks = null;
        Query query = this.getEm().createNamedQuery("findTrackList");
        query.setParameter("nitFabricante", nit);
        
        if(query.getResultList() != null && !query.getResultList().isEmpty()){
            tracks = new ArrayList<Track>();
            tracks = query.getResultList();
            
            for(Track track : tracks){
                trackBO = new TrackBO();
                trackBO = track.TrackBO();
                trackList.add(trackBO);
            }
            
        }

        return trackList;
    }

     public TrackBO buscarTransaccionPorId(String idTransaccion) {
         TrackBO trackBO = new TrackBO();
         Query query = this.getEm().createNamedQuery("findByIdTrack");
         query.setParameter("idTransaccion", idTransaccion);
         
          if(query.getResultList() != null && !query.getResultList().isEmpty()){
              Track track = (Track)query.getResultList().get(0);
              trackBO = track.TrackBO();//validar
          }
         
        return  trackBO;
    }
     
    public List<TrackBO> buscarPorNumSeguimiento(String numSeguimiento) {
        TrackBO trackBO = null;
        List<TrackBO> trackList = new ArrayList<TrackBO>(0);
        List<Track> tracks = null;

        Query query = this.getEm().createNamedQuery("findTrackListByNumSeg");
        query.setParameter("numSeguimiento", numSeguimiento);

        if (query.getResultList() != null && !query.getResultList().isEmpty()) {
            tracks = new ArrayList<Track>();
            tracks = query.getResultList();

            for (Track track : tracks) {
                trackBO = new TrackBO();
                trackBO = track.TrackBO();
                trackList.add(trackBO);
            }

        }

        return trackList;
    }
     
    private Persistence getEm() {
        Persistence persistence = new Persistence();
        return persistence;
    }
}


